﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
//此功能还没成功……
public partial class Amend : System.Web.UI.Page
{
    Xqsys.MODEL.Users user = new Xqsys.MODEL.Users();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["se1"] == null)
        {
            lit.Text = "<script>alert('请登录');</script>";
            Response.Redirect("~/index.aspx");
        }
        else
        {
            loginname.Text = Session["se1"].ToString();
            int userID = Convert.ToInt32(Session["userID"]);
            Xqsys.MODEL.Users user = new Xqsys.MODEL.Users();
            user.UserID = userID;
            DataSet da = new Xqsys.BLL.Users().SelectUserID(user);
            if (Session["Role"].ToString() == "2")//为什么用 “==” 不可以 之后要改回来
            {
                Panel1.Visible = true;
                Panel2.Visible = true;
                Panel3.Visible = true;
            }
            else
            {
                Panel1.Visible = false;
                Panel2.Visible = false;
                Panel3.Visible = false;
            }
            TextBox1.Text = da.Tables[0].Rows[0]["email"].ToString();
            TextBox2.Text = da.Tables[0].Rows[0]["realName"].ToString();
            TextBox3.Text = da.Tables[0].Rows[0]["phone"].ToString();
            TextBox4.Text = da.Tables[0].Rows[0]["address"].ToString();
            TextBox5.Text = da.Tables[0].Rows[0]["introduction"].ToString();
            if (da.Tables[0].Rows[0]["sex"].ToString() == "男")
            {
                RB1.Checked = true;
            }
            else if (da.Tables[0].Rows[0]["sex"].ToString() == "女")
            {
                RB2.Checked = true;
            }
        }
    }
    protected void zhuxiao_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("~/index.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        user.Email = TextBox1.Text.Trim();
        user.RealName = TextBox2.Text.Trim();
        user.Phone = TextBox3.Text.Trim();
        user.Address = TextBox4.Text.Trim();
        user.Introduction = TextBox5.Text.Trim();
        if (RB1.Checked == true)
        {
            user.Sex = "男";
        }
        else if (RB2.Checked == true)
        {
            user.Sex = "女";
        }
        else
        {
            user.Sex = "" ;
        }
        bool str = System.Text.RegularExpressions.Regex.IsMatch(user.Email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        if (str)
        {
            if (new Xqsys.BLL.Users().UpdateUser(user))
            {
                lit.Text = "<script>alert('修改成功')</script>";
            }
            else
            {
                lit.Text = "<script>alert('修改失败')</script>";
            }
        }
        else
        {
            lit.Text = "<script>alert('请输入合法邮箱！')</script>";
        }
    }
    protected void TextBox5_TextChanged(object sender, EventArgs e)
    {

    }
    protected void tijiao_Click(object sender, EventArgs e)
    {
        user.UserID = Convert.ToInt32(Session["userID"]);
        DataSet ds = new Xqsys.BLL.Users().SelectUserID(user);
        string passOld = ds.Tables[0].Rows[0]["password"].ToString();
        if (TextBox6.Text == passOld)
        {
            if (TextBox7.Text == TextBox8.Text)
            {
                if (new Xqsys.BLL.Users().ChangerPwd(user))
                {
                    lit.Text = "<script>alert('修改成功')</script>";
                }
                else
                {
                    lit.Text = "<script>alert('修改失败')</script>";
                }
            }
            else
            {
                lit.Text = "<script>alert('两次密码不一致')</script>";
            }
        }
        else
        {
            lit.Text = "<script>alert('密码错误')</script>";
        }
    }
    protected void RB1_CheckedChanged(object sender, EventArgs e)
    {
        this.RB2.Checked = false; 
    }
    protected void RB2_CheckedChanged(object sender, EventArgs e)
    {
        this.RB1.Checked = false;
    }
}